package com.haidechizi.i18n.code.generator.common.message.key;




import com.haidechizi.i18n.code.generator.common.message.utils.KeyUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 枚举类key生成器
 */
public class EnumKeyGenerator extends BaseKeyGenerator {


    private static final String classNameRegex = "[^\\n]enum[ ]+([0-9a-zA-Z]+)";


    /**
     * 生成key
     *
     * @param name
     * @param fileType
     * @param content
     * @return
     */
    @Override
    public String generatorKey(String name, String fileType, String content) {
        Pattern pattern = Pattern.compile(classNameRegex);
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            String className = matcher.group(1);

            pattern = Pattern.compile("([0-9a-zA-Z_]+).*" + name + ".*");
            matcher = pattern.matcher(content);
            if (matcher.find()) {
                String enumName = matcher.group(1);
                return className + "_" + enumName;

            }
        }
        return KeyUtils.generatorKey();

    }

    /**
     * key generator 名称
     *
     * @return
     */
    @Override
    public String name() {
        return "enum";
    }
}
